﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.Models;
namespace RMS.Controllers
{
    public class ProblemController : Controller
    {
        RMSDataClassesDataContext db = new RMSDataClassesDataContext();


        public ActionResult Overview()
        {
            return View();
        }
        public ActionResult OccurenceChart(int year)
        {
            ViewData["year"] = year;
            return View(db.ProblemGroups);
        }
        public ActionResult CostChart(int year)
        {
            ViewData["year"] = year;
            return View(db.ProblemGroups);
        }
        [HttpPost]
        public ActionResult EditProblemScore(int problemScoreId, FormCollection collection)
        {
            ProblemScore problemScore = db.ProblemScores.FirstOrDefault(x => x.Id == problemScoreId);
            UpdateModel(problemScore);
            db.SubmitChanges();

            return RedirectToAction("Details", new { id = problemScore.ProblemId });
        }

        public ActionResult AddProblemToCompany(int companyId, int problemId)
        {
            db.CompaniesProblems.InsertOnSubmit(new CompaniesProblem
            {
                CompanyId = companyId,
                ProblemId = problemId
            });
            db.SubmitChanges();
            return RedirectToAction("Details", new { id = problemId });

        }

        [HttpPost]
        public ActionResult AddOccurence(int problemId, int userId, DateTime occuredDate,decimal cost)
        {
            db.Occurrences.InsertOnSubmit(new Occurrence
            {
                Date = occuredDate,
                ProblemId = problemId,
                UserId = userId,
                Cost = cost
            });
            db.SubmitChanges();
            return RedirectToAction("Details", new { id = problemId });
        }
        public ActionResult Approve(int id)
        {
            User user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name);
            Problem problem = db.Problems.FirstOrDefault(x => x.Id == id);
            problem.ApprovalUserId = user.Id;
            db.SubmitChanges();
            return RedirectToAction("WaitForApproveList");
        }

        /*
        [HttpPost]
        public ActionResult AddOccurrence(int problemId, DateTime date)
        {
            Occurrence newOccurence = new Occurrence
            {
                Date = date,
                ProblemId = problemId
            };
            db.Occurrences.InsertOnSubmit(newOccurence);
            db.SubmitChanges();
            return RedirectToAction("Details", new { id = problemId });
        }*/
        
        [HttpPost]
        public ActionResult AddScore(decimal discrimination, int problemId, decimal impact, decimal probability, decimal detectionScore)
        {
            ProblemScore newProblemScore = new ProblemScore
            {
                ImpactScore = impact,
                ProbabilityScore = probability,
                ProblemId = problemId,
                DiscriminationScore = discrimination,
                UserId = db.Users.FirstOrDefault(x=>x.UserName == HttpContext.User.Identity.Name).Id,
                DetectionScore = detectionScore
            };
            db.ProblemScores.InsertOnSubmit(newProblemScore);
            db.SubmitChanges();
            return RedirectToAction("Details", new { id = problemId });
        }

        public ActionResult Index()
        {
            User user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name);
            if (user != null)
            {
                ViewData["company"] = user.Company;
            }
            return View(db.Problems);
        }
        public ActionResult Details(int id)
        {
            ViewData["companies"] = db.Companies.AsEnumerable();
            ViewData["bestPracticeGroups"] = db.BestPracticeGroups;

            return View(db.Problems.FirstOrDefault(x=>x.Id == id));
        }
        [HttpPost]
        public ActionResult Decline(int id, string reason)
        {
            User user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name);
            Problem pb = db.Problems.FirstOrDefault(x => x.Id == id);
            pb.ApprovalUserId = user.Id;
            pb.IsDeclined = true;
            pb.Reason = reason;
            db.SubmitChanges();
            return RedirectToAction("Index");
        }
        public ActionResult Decline(int id)
        {
            Problem pb = db.Problems.FirstOrDefault(x => x.Id == id);
            return View(pb);
        }
        public ActionResult WaitForApproveList()
        {
            ViewData["bestPractices"] = db.BestPractices.Where(x => x.IsShared && !x.ApprovalUserId.HasValue && !x.IsDeclined);
            return View(db.Problems.Where(x => x.IsShared && !x.ApprovalUserId.HasValue && !x.IsDeclined));
        }
        public ActionResult RequestApprove(int id)
        {
            Problem problem = db.Problems.FirstOrDefault(x => x.Id == id);
            problem.IsShared = true;
            db.SubmitChanges();
            return RedirectToAction("Index");
        }
        public ActionResult Create(int? productId, int companyId)
        {
            ViewData["userId"] = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name).Id;

            if (productId.HasValue)
            {
                Product product = db.Products.FirstOrDefault(x => x.Id == productId);
                ViewData["productId"] = product.Id;
                ViewData["productName"] = product.Name;
            }
            ViewData["riskTypes"] = db.RiskTypes;
            ViewData["problemGroups"] = db.ProblemGroups.AsEnumerable();
            Company company = db.Companies.FirstOrDefault(x => x.Id == companyId);
            ViewData["company"] = company;

            return View();
        } 
        [HttpPost]
        public ActionResult Create(Problem newProblem,int companyId)
        {
            try
            {
                db.Problems.InsertOnSubmit(newProblem);
                db.SubmitChanges();

                db.CompaniesProblems.InsertOnSubmit(new CompaniesProblem
                {
                    CompanyId = companyId,
                    ProblemId = newProblem.Id
                });
                db.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                if (newProblem.ProductId.HasValue)
                {
                    Product product = db.Products.FirstOrDefault(x => x.Id == newProblem.ProductId);
                    ViewData["productId"] = product.Id;
                    ViewData["productName"] = product.Name;
                }
                Company company = db.Companies.FirstOrDefault(x => x.Id == companyId);
                ViewData["company"] = company;
                ViewData["problemGroups"] = db.ProblemGroups;
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            return View(db.Problems.FirstOrDefault(x => x.Id == id));
        }
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Problem problem = db.Problems.FirstOrDefault(x => x.Id == id);
            try
            {
                UpdateModel(problem);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(problem);
            }
        }
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Problem/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
